package com.ruitu.mall_98_2.mvp.presenter;

import android.text.TextUtils;

import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_2.mvp.contract.LoginContract;
import com.ruitu.router_module.bean.User;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;

public class LoginPresenter extends LoginContract.Presenter {
    @Override
    public void login(String phone, String pwd,String type,String code) {
        mModel.login(phone, pwd,type,code)
                .subscribe(new SubscriberImpl<BaseResult<User>>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        e.printStackTrace();
                        ToastUtils.showShort("登录失败");
                    }

                    @Override
                    public void onNext(BaseResult<User> baseResult) {
                        mView.hideProgress();
                        if (baseResult.isSuccess()) {
                            mView.loginSuc(baseResult.getData());
                            ToastUtils.showShort(baseResult.getMsg());
                        } else {
                            if (TextUtils.equals(baseResult.getCode(),"3")){
                                mView.differentPlace();
                            }
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }

    @Override
    public void quickLogin(String openId, String type) {
        mModel.quickLogin(openId, type)
                .subscribe(new SubscriberImpl<BaseResult<User>>() {
                    @Override
                    public void onError(Throwable e) {
                        Logg.i(e.toString());
                        mView.hideProgress();
                        ToastUtils.showShort("获取第三方登录失败");
                    }

                    @Override
                    public void onNext(BaseResult<User> userBaseResult) {
                        mView.hideProgress();
                        if (userBaseResult.isSuccess()) {
                            mView.thirdLoginSuc(userBaseResult.getData());
                        } else {
                            if (TextUtils.equals(userBaseResult.getCode(), "1")) {
                                mView.setThird();
                            } else {
                                ToastUtils.showShort(userBaseResult.getMsg());
                            }

                        }
                    }
                });
    }
}
